import {combineReducers} from 'redux';
import {rootDom, RootNavigator} from '../navigator/AppNavigator';
import themeReducer from './theme';
import popularReducer from './popular';
import trendingReducer from './trending';

/**
 * 指定默认的 state
 * @type {State | null}
 */
const navState = RootNavigator.router.getStateForAction(RootNavigator.router.getActionForPathAndParams(rootDom));

/**
 * 创建 navigation reducer
 * @param state
 * @param action
 * @returns {State}
 */
const navReducer = (state = navState, action) => {
  const nextState = RootNavigator.router.getStateForAction(action, state);
  // 如果nextState为null或undefined，返回默认的state
  return nextState || state;
};

/**
 * 合并reducer
 * @type {Reducer<any> | Reducer<any, AnyAction>}
 */
const index = combineReducers({
  nav: navReducer,
  theme: themeReducer,
  popular: popularReducer,
  trending: trendingReducer
});

/**
 * 将合并的reducer导出
 */
export default index;